package com.laowang.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.laowang.common.utils.http.HttpKit;
import com.laowang.project.pay.config.MyWxPayConfigUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.KeyManagementException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ExecutionException;

/**
 * @Auther laowang
 * @Date 2022/7/28
 * @Description
 */

@Slf4j
public class PayUtil {

    public static final String requestUrl= "http://testterpay.postar.cn";



    public static void main(String[] args) throws InterruptedException, ExecutionException, NoSuchAlgorithmException, KeyManagementException, NoSuchProviderException, IOException {
        //getCustInfo("wangjian01","800528000020968","15","1.0.0","583567EA36FA617DF704269634E0971A");
        //pubSigPay(null,null,null,null,null,null,null,null);
        //test2();
        //test3();
        //test5();

        //orderQuery(new MyWxPayConfigUtil());
        //MyWxPayConfigUtil myWxPayConfigUtil = new MyWxPayConfigUtil("","800528000020975","7D258A5EA5949FE4EB0B4FB9F5A4C88B","15","88183559",new Date(),"599","1560512968397946880","20220819142419232545","","");
        //refund(myWxPayConfigUtil);
        String str= "0080055100005605932023051009450124547215MD516561130769125212168833515020230510104228V1.0.0F3A9B8509031A381DAF11718326986C1";
        String sign = DigestUtils.md5DigestAsHex(str.getBytes());
        System.out.println(sign);
        //999ad5986ecc490d49ebca37523aa9f2
        //999ad5986ecc490d49ebca37523aa9f2
    }


    public static void test33() throws InterruptedException, ExecutionException,  IOException {

        String key ="583567EA36FA617DF704269634E0971A";
        //String url  ="http://sandbox.starpos.com.cn/adpweb/ehpspos3/pubSigPay.json";
        String url  ="http://gateway.starpos.com.cn/adpweb/ehpspos3/pubSigPay.json";
        String orgNo ="15";  //机构号
        String trmNo ="88179645"; // 设备号  长度 8 位数字，进件  生成终端号

        String mercId="800528000020968"; //商户号

        TreeMap<String, String> params = new TreeMap<>();

        params.put("amount","1");
        //params.put("authCode","130219205322723729");
        //params.put("characterSet","00");
        params.put("mercId",mercId);
        //params.put("opSys","3");
        params.put("orgNo",orgNo);
        //params.put("payChannel","WXPAY");
        params.put("signType","MD5");
        params.put("total_amount","1");
        params.put("tradeNo","20170804103115431252");// 订单号
        params.put("trmNo",trmNo);// 终单号
        params.put("openid","dasdasfdsfsdf");
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = format.format(new Date());
        params.put("txnTime",now);
        params.put("version","V1.0.0");
        //System.out.println(params);
        // 拼接参数，生成sign
        StringBuilder sb = new StringBuilder();
        for (String str : params.keySet()) {
            sb.append(params.get(str));

        }

        String s = sb.toString();
        s = s + key;
        String sign = DigestUtils.md5DigestAsHex(s.getBytes());
        System.out.println(sign);
        params.put("signValue",sign.toLowerCase());
        String s1 = JSONObject.toJSONString(params);
        String result = HttpKit.post(url, s1);
        result =  URLDecoder.decode(result,"UTF-8");
        //String result = HttpKit.post(url, s1);
        //System.out.println("resultMap = " + result);
        Map<String,String> map = JSONObject.parseObject(result, Map.class);
        System.out.println("map:"+map);
        //map:{logNo=202207297128702850, mercId=800528000020968, amount=1, apiAppid=, apiSigntype=, orderNo=, apiTimestamp=, subject=, message=交易失败：当前商户需补齐相关资料后，才可进行相应的支付交易，请商户联系对接的微信支付服务商, result=F, returnCode=000035, apiPackage=, total_amount=1, goods_tag=, PrepayId=, selOrderNo=, sysTime=20220729105342, apiPaysign=, apiNoncestr=}

    }

    /**
     * 支付宝退款接口
     */
    public static Map<String,String> refund(MyWxPayConfigUtil myWxPayConfigUtil) throws InterruptedException, ExecutionException, IOException {
        //MyWxPayConfigUtil myWxPayConfigUtil = new MyWxPayConfigUtil("sdasd","800528000020975","7D258A5EA5949FE4EB0B4FB9F5A4C88B","15","88183559",new Date(),"dddd","1559443311679242240","1","1");
        String url  ="http://gateway.starpos.com.cn/adpweb/ehpspos3/sdkRefundBarcodePay.json";
        TreeMap<String, String> params = new TreeMap<>();
        params.put("orgNo",myWxPayConfigUtil.getOrgNo());
        params.put("opSys","3");
        params.put("mercId",myWxPayConfigUtil.getMchId());
        params.put("trmNo",myWxPayConfigUtil.getTrmNo());// 终单号
        params.put("tradeNo", IdUtils.fastSimpleUUID());//自己的系统 订单号 不能和支付的一样
        params.put("orderNo",myWxPayConfigUtil.getOrderNo());//国通 订单号
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = format.format(myWxPayConfigUtil.getDate());
        params.put("txnTime",now);
        params.put("version","V1.0.0");
        params.put("signType","MD5");
        // 拼接参数，生成sign
        StringBuilder sb = new StringBuilder();
        for (String str : params.keySet()) {
            sb.append(params.get(str));
        }
        String s = sb.toString();
        s = s + myWxPayConfigUtil.getApiKey();
        String sign = DigestUtils.md5DigestAsHex(s.getBytes());
        //System.out.println(sign);
        params.put("signValue",sign.toLowerCase());
        String s1 = JSONObject.toJSONString(params);
        String result = HttpKit.post(url, s1);
        result =  URLDecoder.decode(result,"UTF-8");
        Map<String,String> map = JSONObject.parseObject(result, Map.class);
        System.out.println("map:"+map);
        //map:{logNo=202208197496940059, mercId=800528000020975, amount=1, tradeNo=1559443311679242240, subject=, goodsTag=, message=交易退款成功, signValue=b33d23af6f65fd69a085fdf40d8c54a5, result=S, returnCode=000000, total_amount=1, selOrderNo=, sysTime=20220819112704, attach=, txnAmt=1}
        return map;
    }

    /**
     * 小程序订单查询
     * @return
     */
    public static Map<String,String> orderQuery(MyWxPayConfigUtil myWxPayConfigUtil ) throws IOException, InterruptedException, ExecutionException {

        String key ="7D258A5EA5949FE4EB0B4FB9F5A4C88B";
        String orgNo ="15";  //机构号
        String trmNo ="88183559"; // 设备号  长度 8 位数字，进件  生成终端号
        String mercId="800528000020975"; //商户号

        String url  ="http://gateway.starpos.com.cn/adpweb/ehpspos3/sdkQryBarcodePay.json";
        TreeMap<String, String> params = new TreeMap<>();
        params.put("mercId",mercId /*myWxPayConfigUtil.getMchId()*/);//商户号
        params.put("orgNo",orgNo /*myWxPayConfigUtil.getOrgNo()*/); //机构号
        //params.put("signType","MD5");
        params.put("trmNo",trmNo  /*myWxPayConfigUtil.getTrmNo()*/);// 终端号
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = format.format(new Date() /*myWxPayConfigUtil.getDate()*/);
        params.put("txnTime",now);
        params.put("version","V1.0.0");
        //params.put("qryNo","202208017183824545");//订单号
        params.put("tradeNo","1553947721411330048");//订单号
        StringBuilder sb = new StringBuilder();
        for (String str : params.keySet()) {
            sb.append(params.get(str));

        }

        String s = sb.toString();
        s = s +  key /*myWxPayConfigUtil.getApiKey()*/;
        String sign = DigestUtils.md5DigestAsHex(s.getBytes());
        //System.out.println(sign);
        params.put("signValue",sign.toLowerCase());
        String s1 = JSONObject.toJSONString(params);
        String result = HttpKit.post(url, s1);
        result =  URLDecoder.decode(result,"UTF-8");
        //String result = HttpKit.post(url, s1);
        //System.out.println("resultMap = " + result);
        Map<String,String> map = JSONObject.parseObject(result, Map.class);
        System.out.println("map:"+map);
        // map:{result=F, mercId=800528000020975, returnCode=060003, tradeNo=1553947721411330048, sysTime=20220801133700, message=, signValue=ce15bfbc96cdcbdf11f9c089381d97d6}
        return null;
    }




    /**
     *   支付宝支付
     * @param myWxPayConfigUtil
     * @return
     * @throws InterruptedException
     * @throws ExecutionException
     * @throws IOException
     */
    public static Map<String,String> test6(MyWxPayConfigUtil myWxPayConfigUtil) throws InterruptedException, ExecutionException,  IOException {
        String url  ="http://gateway.starpos.com.cn/adpweb/ehpspos3/aliServicePay.json";
        TreeMap<String, String> params = new TreeMap<>();
        params.put("orgNo",myWxPayConfigUtil.getOrgNo()); //机构号
        params.put("mercId",myWxPayConfigUtil.getMchId());// 商户号
        params.put("trmNo",myWxPayConfigUtil.getTrmNo());// 终单号
        params.put("tradeNo",myWxPayConfigUtil.getOrderId());// 订单号

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = format.format(myWxPayConfigUtil.getDate());
        params.put("txnTime",now);
        params.put("version","V1.0.0");


        //params.put("payChannel","WXPAY");
        params.put("signType","MD5");
        params.put("amount",/*"1"*/myWxPayConfigUtil.getAmount());
        params.put("total_amount",/*"1"*/myWxPayConfigUtil.getTotalAmount());
        params.put("ali_user_id",myWxPayConfigUtil.getOpenId());//买家的支付宝唯一用户号

        //System.out.println(params);
        // 拼接参数，生成sign
        StringBuilder sb = new StringBuilder();
        for (String str : params.keySet()) {
            sb.append(params.get(str));

        }

        String s = sb.toString();
        s = s + myWxPayConfigUtil.getApiKey();
        String sign = DigestUtils.md5DigestAsHex(s.getBytes());
        //System.out.println(sign);
        params.put("signValue",sign.toLowerCase());
        String s1 = JSONObject.toJSONString(params);
        String result = HttpKit.post(url, s1);
        result =  URLDecoder.decode(result,"UTF-8");
        //String result = HttpKit.post(url, s1);
        //System.out.println("resultMap = " + result);
        Map<String,String> map = JSONObject.parseObject(result, Map.class);
        System.out.println("map:"+map);

        return map;
    }


    /**
     *  小程序退款接口
     * @param myWxPayConfigUtil
     * @return
     */
    public static Map<String,String> test5(MyWxPayConfigUtil myWxPayConfigUtil)  {
        String url  ="http://gateway.starpos.com.cn/adpweb/ehpspos3/sdkRefundBarcodePay.json";
        TreeMap<String, String> params = new TreeMap<>();


        params.put("opSys","3");
        params.put("characterSet","00");
        params.put("orgNo",myWxPayConfigUtil.getOrgNo()); //机构号
        params.put("mercId",myWxPayConfigUtil.getMchId());// 商户号
        params.put("trmNo",myWxPayConfigUtil.getTrmNo());// 终单号

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = format.format(myWxPayConfigUtil.getDate());
        params.put("txnTime",now);
        params.put("version","V1.0.0");
        params.put("signType","MD5");
        params.put("orderNo",myWxPayConfigUtil.getOrderNo());
        params.put("tradeNo", myWxPayConfigUtil.getOrderId());//自己的系统 订单号 不能和支付的一样

        //System.out.println(params);
        // 拼接参数，生成sign
        StringBuilder sb = new StringBuilder();
        for (String str : params.keySet()) {
            sb.append(params.get(str));

        }

        String s = sb.toString();
        s = s + myWxPayConfigUtil.getApiKey();
        //System.out.println("代签名:"+s);
        String sign = DigestUtils.md5DigestAsHex(s.getBytes());
        params.put("signValue",sign.toLowerCase());
        String s1 = JSONObject.toJSONString(params);
        //System.out.println("s1:"+s1);
        String result = null;
        try {
            result = HttpKit.post(url, s1);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            result =  URLDecoder.decode(result,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Map<String,String> map = JSONObject.parseObject(result, Map.class);
        log.error("退款map:"+map );

        return map;
    }



    /**
     * 小程序支付接口
     * @param myWxPayConfigUtil
     * @return
     * @throws InterruptedException
     * @throws ExecutionException
     * @throws IOException
     */
    public static Map<String,String> test4(MyWxPayConfigUtil myWxPayConfigUtil) throws InterruptedException, ExecutionException,  IOException {

        String url  ="http://gateway.starpos.com.cn/adpweb/ehpspos3/pubSigPay.json";
        TreeMap<String, String> params = new TreeMap<>();

        params.put("amount", myWxPayConfigUtil.getTotalAmount() /*"1"*/);
        params.put("mercId",myWxPayConfigUtil.getMchId());
        params.put("orgNo",myWxPayConfigUtil.getOrgNo());
        params.put("signType","MD5");
        params.put("total_amount", myWxPayConfigUtil.getTotalAmount()/*"1"*/);
        params.put("txnappid",myWxPayConfigUtil.getAppId());
        params.put("tradeNo",myWxPayConfigUtil.getOrderId());// 订单号
        params.put("trmNo",myWxPayConfigUtil.getTrmNo());// 终单号
        params.put("openid",myWxPayConfigUtil.getOpenId());
        params.put("reqip",myWxPayConfigUtil.getIpAddr());
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = format.format(myWxPayConfigUtil.getDate());
        params.put("txnTime",now);
        params.put("version","V1.0.0");
        //System.out.println(params);
        // 拼接参数，生成sign
        StringBuilder sb = new StringBuilder();
        for (String str : params.keySet()) {
            sb.append(params.get(str));

        }

        String s = sb.toString();
        //System.out.println("待签名原文:"+s);
        s = s + myWxPayConfigUtil.getApiKey();
        String sign = DigestUtils.md5DigestAsHex(s.getBytes());
        //System.out.println(sign);
        params.put("signValue",sign.toLowerCase());
        String s1 = JSONObject.toJSONString(params);
        System.out.println("报文:"+s1);
        String result = HttpKit.post(url, s1);
        result =  URLDecoder.decode(result,"UTF-8");
        //String result = HttpKit.post(url, s1);
        //System.out.println("resultMap = " + result);
        Map<String,String> map = JSONObject.parseObject(result, Map.class);
        log.error("小程序支付map:{}",map);
        // 小程序支付map:{logNo=202305101006811424,
        // mercId=800551000056059, amount=1,
        // apiAppid=wx631bb9fbc73baa08, apiSigntype=RSA,
        // orderNo=20230510094501245472, apiTimestamp=1683683101,
        // subject=, message=等待支付, result=A, returnCode=000000,
        // apiPackage=prepay_id=wx100945017071476c6854d7662657fb0000,
        // total_amount=1, goods_tag=, PrepayId=wx100945017071476c6854d7662657fb0000,
        // selOrderNo=, sysTime=20230510094501,
        // apiPaysign=lYrrCt8l9gSLPnAOV3PLEjFfazoeOCMJUKW9TAiL83GvWgXVlX1ZPgLo586JP33ko6k1ii8UaURT1EbxkartxjJyhx0q6Q3FmTfzW9WUVCTIaU7nvOVNIF/8WbtIRfiNh/swny4LT62gyxhuXuxJWcdNmzLJneb4KBXmANwAO5nblJxmorvXlWTT4IugML KML5qPUFtgvAdqqqlrkaBbZKyEwHjDBXb/fKEIfG6spEAOpOotO6i4wkYiqyZAPRL//iZPxw3RQxI6EOGBCTEnN5AacBQK82YEfsA7AccVMRSxU/eaPiT5Fnc9F1CN5S8BYEiykAX5Dt z4Lcyh9QOg==,
        // apiNoncestr=43f61ecde4b14f3a97cc92fb80912f11}
        return map;
    }



    public static void test3() throws InterruptedException, ExecutionException,  IOException {

        String key ="583567EA36FA617DF704269634E0971A";
        //String url  ="http://sandbox.starpos.com.cn/adpweb/ehpspos3/pubSigPay.json";
        String url  ="http://gateway.starpos.com.cn/adpweb/ehpspos3/pubSigPay.json";
        String orgNo ="15";  //机构号
        String trmNo ="88179645"; // 设备号  长度 8 位数字，进件  生成终端号

        String mercId="800528000020968"; //商户号

        TreeMap<String, String> params = new TreeMap<>();

        params.put("amount","1");
        //params.put("authCode","130219205322723729");
        //params.put("characterSet","00");
        params.put("mercId",mercId);
        //params.put("opSys","3");
        params.put("orgNo",orgNo);
        //params.put("payChannel","WXPAY");
        params.put("signType","MD5");
        params.put("total_amount","1");
        params.put("tradeNo","20170804103115431252");// 订单号
        params.put("trmNo",trmNo);// 终单号
        params.put("openid","dasdasfdsfsdf");
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = format.format(new Date());
        params.put("txnTime",now);
        params.put("version","V1.0.0");
        //System.out.println(params);
        // 拼接参数，生成sign
        StringBuilder sb = new StringBuilder();
        for (String str : params.keySet()) {
            sb.append(params.get(str));

        }

        String s = sb.toString();
        s = s + key;
        String sign = DigestUtils.md5DigestAsHex(s.getBytes());
        System.out.println(sign);
        params.put("signValue",sign.toLowerCase());
        String s1 = JSONObject.toJSONString(params);
        String result = HttpKit.post(url, s1);
        result =  URLDecoder.decode(result,"UTF-8");
        //String result = HttpKit.post(url, s1);
        //System.out.println("resultMap = " + result);
        Map<String,String> map = JSONObject.parseObject(result, Map.class);
        System.out.println("map:"+map);
        //map:{logNo=202207297128702850, mercId=800528000020968, amount=1, apiAppid=, apiSigntype=, orderNo=, apiTimestamp=, subject=, message=交易失败：当前商户需补齐相关资料后，才可进行相应的支付交易，请商户联系对接的微信支付服务商, result=F, returnCode=000035, apiPackage=, total_amount=1, goods_tag=, PrepayId=, selOrderNo=, sysTime=20220729105342, apiPaysign=, apiNoncestr=}

    }

    public static void test5() throws InterruptedException, ExecutionException,  IOException {

        String key ="7D258A5EA5949FE4EB0B4FB9F5A4C88B";
        //String url  ="http://sandbox.starpos.com.cn/adpweb/ehpspos3/pubSigPay.json";
        String url  ="http://gateway.starpos.com.cn/adpweb/ehpspos3/pubSigPay.json";
        String orgNo ="15";  //机构号
        String trmNo ="88183559"; // 设备号  长度 8 位数字，进件  生成终端号

        String mercId="800528000020975"; //商户号

        TreeMap<String, String> params = new TreeMap<>();

        params.put("amount","1");
        //params.put("authCode","130219205322723729");
        //params.put("characterSet","00");
        params.put("mercId",mercId);
        //params.put("opSys","3");
        params.put("orgNo",orgNo);
        //params.put("payChannel","WXPAY");
        params.put("signType","MD5");
        params.put("total_amount","1");
        params.put("tradeNo","20170804103115431252");// 订单号
        params.put("trmNo",trmNo);// 终单号
        params.put("openid","o4NA25OpbvoNzV-RA7A86j7OynTw");
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = format.format(new Date());
        params.put("txnTime",now);
        params.put("version","V1.0.0");
        //System.out.println(params);
        // 拼接参数，生成sign
        StringBuilder sb = new StringBuilder();
        for (String str : params.keySet()) {
            sb.append(params.get(str));

        }

        String s = sb.toString();
        s = s + key;
        String sign = DigestUtils.md5DigestAsHex(s.getBytes());
        System.out.println(sign);
        params.put("signValue",sign.toLowerCase());
        String s1 = JSONObject.toJSONString(params);
        String result = HttpKit.post(url, s1);
        result =  URLDecoder.decode(result,"UTF-8");
        //String result = HttpKit.post(url, s1);
        //System.out.println("resultMap = " + result);
        Map<String,String> map = JSONObject.parseObject(result, Map.class);
        System.out.println("map:"+map);
        //map:{logNo=202208017182057662, mercId=800528000020975, amount=1, apiAppid=wx81fa2e99c4fe9a2f, apiSigntype=RSA, orderNo=20220801094608298818, apiTimestamp=1659318369, subject=, message=等待支付, result=A, returnCode=000000, apiPackage=prepay_id=wx01094609241237f70ac2e190c3ed7e0000, total_amount=1, goods_tag=, PrepayId=wx01094609241237f70ac2e190c3ed7e0000, selOrderNo=, sysTime=20220801094608, apiPaysign=me6S42Mr5Jjw7ZHuhhVogSklxRLL4DpqenWIuTzjEv4fYrJidj4rhSLjKwMQYKmDeACzGECcqg0fB IxhueohUgbZDc7b9jkfMjQJPQ0GdYrXUVr7rfX KWqDh4bjRiFHTGJppWdvVS62aUw9zVuvR/oSor/thzef WiHR7P doASCYJx6OZYm2docqe5WAG7LLOnQiVxvtAjPloMe0CTXOmh703ps8O40NSGV0Jk5Q19hScXGSrOzcNfz7h/jR/GsQCScXvpsRu5kp0PBsIiXXiOIGsFfO47pIQi5Pu6Gz1uCMMgsPbUJEIhJvs415cdP5j2pEyl1RNKOttww1x0g==, apiNoncestr=4a748ec90afd477188ee021d87b54e06}

    }



    public static void test2() throws InterruptedException, ExecutionException, NoSuchAlgorithmException, KeyManagementException, NoSuchProviderException, IOException {

        //String key ="583567EA36FA617DF704269634E0971A";
        String key ="C286D0E61C6989596A23B654FDCAB361";
        String jinjian_key ="AFDFAASDASDAS";
        String url  ="http://sandbox.starpos.com.cn/adpweb/ehpspos3/sdkBarcodePay.json";
        String orgNo ="518";
        String trmNo ="XB033766"; // 终单号
        String mercId="800290000015948";

        TreeMap<String, String> params = new TreeMap<>();

        params.put("amount","1");
        params.put("authCode","130219205322723729");
        params.put("characterSet","00");
        params.put("mercId",mercId);
        params.put("opSys","3");
        params.put("orgNo",orgNo);
        params.put("payChannel","WXPAY");
        params.put("signType","MD5");
        params.put("total_amount","1");
        params.put("tradeNo","20170804103115431251");// 订单号
        params.put("trmNo",trmNo);// 终单号
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = format.format(new Date());
        params.put("txnTime","20170804103115");
        params.put("version","V1.0.0");
        //System.out.println(params);
        // 拼接参数，生成sign
        StringBuilder sb = new StringBuilder();
        for (String str : params.keySet()) {
            sb.append(params.get(str));

        }

        String s = sb.toString();
        s = s + key;
        String sign = DigestUtils.md5DigestAsHex(s.getBytes());
        System.out.println(sign);
        params.put("signValue",sign.toLowerCase());
        String s1 = JSONObject.toJSONString(params);
        //String result =  URLDecoder.decode(HttpKit.post(url, s1),"UTF-8");
        String result = HttpKit.post(url, s1);
        System.out.println("resultMap = " + result);


    }


    public static void test1() throws InterruptedException, ExecutionException, NoSuchAlgorithmException, KeyManagementException, NoSuchProviderException, IOException {

        //String key ="583567EA36FA617DF704269634E0971A";
        String key ="Pz5meIuUxRKcF7rlvPglliyfwvC9vhjGcBy61WHM00Qfwz0E6yTTLwgxJsFYE9IQ";
        String jinjian_key ="AFDFAASDASDAS";
        String url  ="http://sandbox.starpos.com.cn/adpweb/ehpspos3/sdkBarcodePay.json";
        String orgNo ="518";
        String trmNo ="XB033766"; // 终单号
        String mercId="800290000015948";

        TreeMap<String, String> params = new TreeMap<>();

        params.put("amount","1");
        params.put("authCode","130219205322723729");
        params.put("characterSet","00");
        params.put("mercId",mercId);
        params.put("opSys","3");
        params.put("orgNo",orgNo);
        params.put("payChannel","WXPAY");
        params.put("signType","MD5");
        params.put("total_amount","1");
        params.put("tradeNo","20170804103115431251");// 订单号
        params.put("trmNo",trmNo);// 终单号
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = format.format(new Date());
        params.put("txnTime","20170804103115");
        params.put("version","V1.0.0");
        //System.out.println(params);
        // 拼接参数，生成sign
        StringBuilder sb = new StringBuilder();
        for (String str : params.keySet()) {
            sb.append(params.get(str));

        }
        /*String s ="1130219205322723729008002900000004413489WXPAYMD5120170804103115431251950\n" +
            "0688820170804103115V1.0.0Pz5meIuUxRKcF7rlvPglliyfwvC9vhjGcBy61WHM00Qfwz0E6yT\n" +
            "TLwgxJsFYE9IQ";*/




        String s = sb.toString();
        s = s + key;
        String sign = DigestUtils.md5DigestAsHex(s.getBytes());
        System.out.println(sign);
        params.put("signValue",sign.toLowerCase());
        String res = HttpKit.post(url, params);
        System.out.println(res);

    }



    /**
     *  签名机制如下：
     * 1. 签名字段不参与组成签名原文串。
     * 2. 待签名原文是请求参数按参数名字母小写后进行升序排列,对应参数值拼
     * 装成的字符串
     * 3. 如果提交的参数为{apam:value1, cpam:value2, Bpam:value3}
     * 4. 设备 key（交易接口 key 为商户秘钥，进件接口 key 则为机构秘钥）值
     * 为 AFDFAASDASDAS
     * 则待签名原文为：value1value3value2AFDFAASDASDAS
     */
    public static void test(){

        String key ="583567EA36FA617DF704269634E0971A";
        String jinjian_key ="AFDFAASDASDAS";
        String url  ="http://sandbox.starpos.com.cn/adpweb/ehpspos3/sdkBarcodePay.json";
        String orgNo ="518";
        String trmNo ="XB033766";
        String mercId="800290000015948"; //商户号

        TreeMap<String, Object> params = new TreeMap<>();

        params.put("opSys","3");
        params.put("characterSet","00");
        params.put("orgNo",orgNo);
        params.put("mercId",mercId);
        params.put("trmNo",trmNo); //设备终端号
        params.put("tradeNo","15922333");// 订单号
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = format.format(new Date());
        params.put("txnTime",now);
        params.put("signType","MD5");
        params.put("version","V1.0.0");
        params.put("amount","1");
        params.put("total_amount","1");
        System.out.println(params);




    }


    /**
     *  公众号/小程序支付(pubSigPay)
     * @param orgNo  机构号
     * @param mercId  商户号
     * @param trmNo  设备号 长度 8 位数字，进件生成终端号
     * @param tradeNo   在同一天同一商户同 终端下不重复
     * @param txnTime   设备端交易时间 如：20170527153245
     * @param version   V1.0.0
     * @param amount    以分为单位，如 1 元表示为 100
     * @param total_amount   以分为单位，如 1 元表示为 100
     */
    public static void pubSigPay(String orgNo,String mercId,String trmNo,String tradeNo,String txnTime,String version,String amount,String total_amount){
        String url ="http://gateway.starpos.com.cn/adpweb/ehpspos3/pubSigPay.json";
        TreeMap<String,String> params = new TreeMap<>();
        params.put("orgNo","15");
        params.put("mercId","800528000020968");
        params.put("trmNo","XC614373"); //设备终端号
        params.put("tradeNo","15922333");// 订单号
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = format.format(new Date());
        params.put("txnTime",now);
        params.put("version","V1.0.0");
        params.put("amount","1");
        params.put("total_amount","1");
        try {
            String res = HttpKit.get(url, params);
            System.out.println("res:"+res);
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }




    /**
     *  获取商户信息
     * @param custLogin    登入号
     * @param custId     	商户编号
     * @param agetId      	机构号
     * @param version      	版本号
     * @param sign          验签
     */
    public static void getCustInfo(String custLogin,String custId,String agetId,String version,String sign){
        Map<String,String> params = new HashMap<>();
        params.put("custLogin",custLogin);
        params.put("custId",custId);
        params.put("agetId",agetId);
        params.put("version",version);
        params.put("sign",sign);
        try {
            String res = HttpKit.get(requestUrl, params);
            System.out.println("res:"+res);
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


    /**
     * 利用java原生的类实现SHA256加密
     *
     * @param str 加密后的报文
     * @return
     */
    public static String getSHA256(String str) {
        MessageDigest messageDigest;
        String encodestr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodestr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encodestr;
    }
    /**

     将byte转为16进制



     @param bytes

     @return

     */

    private static String byte2Hex(byte[] bytes) {

        StringBuffer stringBuffer = new StringBuffer();

        String temp = null;

        for (int i = 0; i < bytes.length; i++) {

            temp = Integer.toHexString(bytes[i] & 0xFF);

            if (temp.length() == 1) {

                stringBuffer.append("0");

            }

            stringBuffer.append(temp);

        }

        return stringBuffer.toString();

    }


}
